package com.itserver.controller.common;


import com.itserver.ano.Log;
import com.itserver.pojo.*;
import com.itserver.service.sys.LogService;
import com.itserver.utils.Config;
import com.itserver.utils.JwtUtils;
import com.itserver.utils.RedisCache;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Scope("prototype")
@RestController
@RequestMapping("/sys")
public class LogController {

    @Autowired
    private  LogService logService;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private Config config;

    public LogController(LogService logService) {
        this.logService = logService;
    }

    @GetMapping("/log")
    public Result logList( QueryLog queryLog){
        PageResult<OperateLog> pageResult = logService.logList(queryLog);
        return Result.success(pageResult);
    }
    @DeleteMapping("/log")
    public Result deleteLog(Integer[] ids){
        logService.deleteLog(ids);
        return Result.success();
    }

    // 创建一个静态内部类，用作在线用户类
    @Data
    public static class LUser {
        private String username;
        private Integer page = 1;
        private Integer pageSize = 10;
    }
    /*
     * 获取当前系统在线用户数量
     * */
    @GetMapping("/onLine")
    public Result getOnline(LUser queryLog) {
        List<Map <String, Object>> onlineUser = redisCache.getLoginUserList();
        List< Object > onlineUsers = new ArrayList<>();
        // 循环列表解析token
        for (Map<String, Object> userItem : onlineUser) {
            Map<String, Object> claims = JwtUtils.parseJWT(userItem.get("token").toString());
            if(claims != null){
                long loginTime = (long)(claims.get("exp")) - config.getExpire() * 60;
                Map<String, Object> user  = new HashMap<>();
                user.put("username", claims.get("username"));
                user.put("loginTime", loginTime);
                user.put("id", claims.get("id"));
                user.put("uuid", claims.get("uuid"));
                onlineUsers.add( user );
            }
        }

        // 根据查询条件 username 模糊过滤
        if(queryLog.getUsername() != null && !queryLog.getUsername().isEmpty()) {
            List<Object> list = new ArrayList<>();
            for (Object user : onlineUsers) {
                if (user.toString().contains(queryLog.getUsername())){
                    list.add(user);
                }
            }
            onlineUsers = list;
        }
        // 根据 查询分页 截取列表
        int start = (queryLog.getPage() - 1) * queryLog.getPageSize();
        int end = Math.min(start + queryLog.getPageSize(), onlineUsers.size());
        List< Object> onlineUserList =  onlineUsers.subList(start, end);
        PageResult<Object> pageResult = new PageResult<>((long)onlineUsers.size(),onlineUserList);
        return Result.success(pageResult);
    }

    @Log
    @DeleteMapping("/offLine")
    public Result offLine(String uuid) {
        redisCache.deleteLoginUser(uuid);
        return Result.success();
    }
}
